<?php
    session_start();
    require_once ("../connectDB.php");
    require_once("captcha.php");
    $captcha = new captcha;
    $db = new connectDB;

    // opens the file, and gives an error message if it fails
    if ($_POST["comment"] == "") {
        $_SESSION["message"] = ("ERROR: field 'comment' is empty<br />");
        header('location:../../index.php?page=guestbook');		
    }
    // checks if comment isn't empty

    if($_POST['answerField'] !== ""){
        header('location:../../index.php?page=guestbook');
    }
    // honeypot
    
    $dateday = date('l');
    $datedayno = date('j');
    $datemonth = date('F');
    $dateyear = date('Y');
    $datehourNL = date('G');
    $datehourEN = date('g');
    $datemin = date('i');
    $dateAM = date('A');
    // the different parts of the time. the english time is given in AM/PM, the dutch one in a 24 hour style
    // hence the two different datehour variables

    if($_SESSION["language"] == 0||$_SESSION["language"] == "NL"){
        switch ($dateday)
        {
            case 'Monday':
                $dateday = "Maandag";
                break;
            case 'Tuesday':
                $dateday = "Dinsdag";
                break;
            case 'Wednesday':
                $dateday = "Woensdag";
                break;
            case 'Thursday':
                $dateday = "Donderdag";
                break;
            case 'Friday':
                $dateday = "Vrijdag";
                break;
            case 'Saturday':
                $dateday = "Zaterdag";
                break;
            case 'Sunday':
                $dateday = "Zondag";
                break;
        }
        // this switch converts the english daynames to dutch ones

        $date = ($dateday . " " . $datedayno . " " . $datemonth . " " . $dateyear);
        $time = $datehourNL . "." . $datemin;
    }
    else{
        $date = ($dateday . " " . $datemonth . " " . $datedayno . " " . $dateyear);
        $time = $datehourEN . "." . $datemin . " " . $dateAM;
    }
    // a different time format for each language

    $string = strlen($_POST["name"]);
    $string > 2? $nameorip = $_POST["name"]: $nameorip = $_SERVER["REMOTE_ADDR"];
    // if the name isn't set, an IP will be used instead


    if ($_POST['comment'] !== "ERROR: field 'comment' is empty<br />"){
        $mssg = mysql_escape_string($_POST['comment']);
        $name = mysql_real_escape_string($nameorip);

        if(!$captcha->checkCaptcha()){
            $_SESSION["message"] = ("wrong CAPTCHA.<br />");
        }
        else{
            $SQL = "INSERT INTO guestbook_entry (date, time, name, message) VALUES ('" . $date . "', '" . $time . "', '" . $name . "', '" . $mssg . "')";
            if(!$query = mysql_query($SQL)){
                $_SESSION["message"] = ("content error: writing refused.<br />" . $towrite . "<br />"); 
            }
        }
    }
    header('location:../../index.php?page=guestbook');	
?>